package com.adams.utils;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.UUID;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.SQLExec;
import org.apache.tools.ant.types.EnumeratedAttribute;

import com.adams.core.exception.BusinessException;

/**
 * 
 * File Name : SQLScriptUtil.java
 * 
 * @Description : sql脚本工具类
 * @author 904370 张汉辉
 */
public class SQLScriptUtil
{

	private static Logger logger = Logger.getLogger(SQLScriptUtil.class
			.getName());

	/**
	 * Description : 执行SQL脚本文件
	 * 
	 * @throws BusinessException
	 * 
	 */
	public static void ExecuteSqlScript(String driverClassName,
			Properties properties, String sqlScript) throws BusinessException
	{
		if (StringUtils.isBlank(sqlScript))
		{
			return;
		}
		try
		{
			ExecuteSql(driverClassName, properties, sqlScript);
		} catch (BuildException e)
		{
			logger.error("sql脚本执行出错！", e);
			// 执行脚本失败
			throw new BusinessException("sql.script.exec.error");
		} catch (IOException e)
		{
			logger.error("sql脚本执行出错！", e);
			// 执行脚本失败
			throw new BusinessException("sql.script.exec.error");
		} catch (SQLException e)
		{
			logger.error("sql脚本执行出错！", e);
			// 执行脚本失败
			throw new BusinessException("sql.script.exec.error");
		} catch (Exception e)
		{
			logger.error("sql脚本执行出错！", e);
			// 执行脚本失败
			throw new BusinessException("sql.script.exec.error");
		}
	}

	private static void ExecuteSql(String driverClassName,
			Properties properties, String sqlScript) throws Exception
	{
		File printFile = null;
		try
		{
			SQLExec sqlExec = new SQLExec();
			// 设置数据库参数
			String url = properties.getProperty("url");
			String user = properties.getProperty("user");
			String password = properties.getProperty("password");
			sqlExec.setDriver(driverClassName);
			sqlExec.setUrl(url);
			sqlExec.setUserid(user);
			sqlExec.setPassword(password);
			// 要执行的脚本
			// srcFile = createTempFileBySqlScript(sqlScript);
			// sqlExec.setSrc(srcFile);
			sqlExec.addText(sqlScript);
			// 有出错的语句该如何处理
			sqlExec.setOnerror((SQLExec.OnError) (EnumeratedAttribute
					.getInstance(SQLExec.OnError.class, "abort")));
			try
			{
				printFile = File.createTempFile(UUID.randomUUID().toString(),
						".log");

			} catch (IOException e)
			{
				printFile = null;
			}
			// 设置是否输出
			sqlExec.setPrint(true);
			// 输出到文件 sql.out 中；不设置该属性，默认输出到控制台
			if (null != printFile)
			{
				sqlExec.setOutput(printFile);
			}
			sqlExec.setProject(new Project());
			sqlExec.execute();
		} finally
		{
			if (null != printFile)
			{
				printFile.deleteOnExit();
			}
		}
	}

	// 创建脚本临时文件
	// private static File createTempFileBySqlScript(String sqlScript)
	// throws IOException
	// {
	// File tempFile = File.createTempFile(UUID.randomUUID().toString(),
	// ".sql");
	// FileWriter fw = new FileWriter(tempFile);
	// BufferedWriter bw = new BufferedWriter(fw);
	// bw.write(sqlScript);
	// bw.close();
	// fw.close();
	// return tempFile;
	// }
}
